
let  VSHADER_SOURCE = 
	'attribute vec4 a_Position;\n' + 
	'attribute float a_PointSize;\n' + 
	'void main() {\n' + 
	' 	gl_Position = a_Position;\n' + //设置坐标
	' 	gl_PointSize = a_PointSize;\n' +  //设置尺寸
	'}\n';

let FAHADER_SOURCE = 
	'void main() {\n' + 
	'	gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n' + //设置颜色
	'}\n';


function main() {
	// body...
	let canvas = document.getElementById('webgl');

	let gl = getWebGLContext(canvas);


	if (!initShaders(gl, VSHADER_SOURCE, FAHADER_SOURCE)) {
		console.log("Failed to initialize shaders");
		return;
	}

	let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
	let a_PointSize= gl.getAttribLocation(gl.program, 'a_PointSize');

	if (a_Position < 0) {
		console.log("Failed to get the storage location of a_Position");
		return;
	}

	gl.vertexAttrib1f(a_PointSize, 20.0);

	if (a_PointSize < 0) {
		console.log("Failed to get the storage location of a_PointSize");
		return;
	}

	canvas.onmousedown = function(ev) {
		click(ev, gl, canvas, a_Position);
	};

	gl.clearColor(0.0, 0.0, 0.0, 1.0);
	gl.clear(gl.COLOR_BUFFER_BIT);
}


let g_points = [];  //鼠标点击位置数组


function click(ev, gl, canvas, a_Position) {
  
  	let x = ev.clientX; // x coordinate of a mouse pointer
  	let y = ev.clientY; // y coordinate of a mouse pointer
  	let rect = ev.target.getBoundingClientRect() ;

  	x = ((x - rect.left) - canvas.width/2)/(canvas.width/2);
  	y = (canvas.height/2 - (y - rect.top))/(canvas.height/2);
  	// Store the coordinates to g_points array
  	g_points.push([x, y ]);
  	// Clear <canvas>
  	gl.clear(gl.COLOR_BUFFER_BIT);

  	let len = g_points.length;
  	for(let i = 0; i < len; i ++) {
  		let xy = g_points[i];
    	// Pass the position of a point to a_Position variable
    	gl.vertexAttrib3f(a_Position, xy[0], xy[1], 0.0);

    	// Draw
   		gl.drawArrays(gl.POINTS, 0, 1);
  }

}